package com.servlet;

import com.dao.imp.FindOrder;
import net.sf.json.JSONArray;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Mathison
 * @Title: ${NAME}
 * @Package: ${PACKAGE_NAME}
 * @Description: TODO
 * @date 2021/12/2 19:52
 **/

@WebServlet(name = "GetUserOrderServlet", value = "/GetUserOrderServlet")
public class GetUserOrderServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        HttpSession session = request.getSession();
        int user_id = Integer.parseInt(session.getAttribute("userId").toString());
        int choosePageNum = Integer.parseInt(request.getParameter("choosePage"));
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        int realStartIndex = (choosePageNum-1)*pageSize;
        response.setContentType("text/html;charset=utf-8");

//        String selectSQL = "SELECT * FROM orders WHERE isPay = 1 ORDER BY `order_id` LIMIT "+realStartIndex+","+pageSize;
        String sql = "SELECT * FROM orders WHERE user_id="+user_id+" ORDER BY `order_id` LIMIT "+realStartIndex+","+pageSize;
//        System.out.println(sql);;
        List orderList = FindOrder.getOrderList(sql);
        JSONArray json =JSONArray.fromObject(orderList);
        response.getWriter().print(json);
    }
}
